<template>
  <el-dialog
    title="检查机房"
    :visible.sync="dialogVisible"
    :destroy-on-close="true"
    width="50%"
    :before-close="handleClose">
    <el-form class="rule-form" ref="form" :model="ruleForm" :rules="rules" style="margin-top:40px">
      <el-form-item label="医院" label-width="110px" prop="hospitalCode">
        <el-select style="width:100%" size="small" v-model="ruleForm.hospitalCode" @change="changeHosp" placeholder="请选择医院" filterable clearable>
          <el-option
            v-for="item in mx_hospitalList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="影像区域" label-width="110px" prop="depArea">
        <el-select style="width:100%" size="small" v-model="ruleForm.depArea" @change="changeDepArea" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in depAreaOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室" label-width="110px">
        <el-select style="width:100%" size="small" v-model="ruleForm.department" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in departmentOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查部门" label-width="110px" prop="dep">
        <el-select style="width:100%" size="small" v-model="ruleForm.dep" @change="changeDep" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in depOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查设备" label-width="110px" prop="modality">
        <el-select style="width:100%" size="small" v-model="ruleForm.modality" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in modalityOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查分组" label-width="110px" prop="checkGroup">
        <el-select style="width:100%" size="small" v-model="ruleForm.checkGroup" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in checkGroupOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查机房" label-width="110px" prop="checkRoom">
        <el-select style="width:100%" size="small" v-model="ruleForm.checkRoom" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in checkRoomOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机房状态" label-width="110px" prop="checkRoomState">
        <el-select style="width:100%" size="small" v-model="ruleForm.checkRoomState" placeholder="请选择" clearable filterable allow-create>
          <el-option
            v-for="item in options.checkRoomStateOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="AETITLE" label-width="110px" prop="aeTitle">
        <el-input size="small" v-model="ruleForm.aeTitle" clearable></el-input>
      </el-form-item>
      <el-form-item label="检查地点" label-width="110px" prop="modalityAddress">
        <el-input size="small" v-model="ruleForm.modalityAddress" clearable></el-input>
      </el-form-item>
      <el-form-item label="注意事项" label-width="110px" prop="remark1">
        <el-input size="small" v-model="ruleForm.remark1" clearable></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button size="small" type="primary" @click="handleConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { Options } from '@/utils/constant'
import { addCheckroom, editCheckroom,
  getDepAreaList, getCheckRoomList, getModalityList, getCheckGroupList, getDepList, getItemList } from '@/api/api.js'

export default {
  name: 'CheckRoomDialog',
  props: {
    dialogVisible: {
      default: false,
      type: Boolean
    },
    checkData: {
      default: undefined,
      type: Object
    }
  },
  data() {
    return {
      options: Options,
      ruleForm: {
        checkRoom: '',
        dep: '',
        modality: '',
        checkRoomState: '',
        checkGroup: '',
        depArea: '',
        hospitalCode: '',
        hospitalName: '',
        aeTitle: '',
        department: '',
        modalityAddress: '',
        remark1: ''
      },
      rules: {
        hospitalCode: [{ required: true, message: '请选择医院', trigger: 'blur' }],
        // department: [{ required: true, message: '请选择科室', trigger: 'blur' }],
        checkRoom: [{ required: true, message: '请输入检查机房', trigger: 'blur' }],
        aeTitle: [{ required: true, message: '请输入aeTitle', trigger: 'blur' }],
        dep: [{ required: true, message: '请选择检查部门', trigger: 'blur' }],
        depArea: [{ required: true, message: '请选择影像区域', trigger: 'blur' }],
        modality: [{ required: true, message: '请输入设备', trigger: 'blur' }],
        checkRoomState: [{ required: true, message: '请选择机房状态', trigger: 'blur' }],
        checkGroup: [{ required: true, message: '请选择检查分组', trigger: 'blur' }]
      },
      depAreaOptions: [], // 影像区域
      departmentOptions: [], // 科室
      depOptions: [], // 检查部门
      modalityOptions: [], // 检查设备
      checkGroupOptions: [], // 检查分组
      checkRoomOptions: [], // 检查机房
      initalFlag: true// 判断是否是初始打开界面
    }
  },
  watch: {
    'dialogVisible': {
      immediate: true,
      handler(val) {
        if (val) {
          // 如果存在，是修改，进行赋值
          if (this.checkData.id) {
            this.ruleForm = { ...this.checkData }
          }
          this.init()
          // else{
          //   //是新增，进行清空
          //   this.handleReset()
          // }
        }
      }
    }
  },
  methods: {
    init() {
      this.initalFlag = true
      this.$getHospitalList()
      this.ruleForm.hospitalCode = this.mx_hospitalList[0].code || ''
      this.changeHosp()
    },
    changeHosp() {
      if (!this.ruleForm.hospitalCode) return
      // 关联：影像、检查部门、检查分组、检查设备、检查机房
      let data = { HospitalCode: this.ruleForm.hospitalCode }
      getDepAreaList(data).then(res => {
        if (res && res.code === 10000) {
          this.depAreaOptions = res.content.list
          if (this.initalFlag) {
            // 如果进来尚无更改，默认是this.ruleForm.depArea
            this.ruleForm.depArea = this.ruleForm.depArea || this.depAreaOptions[0]
          } else {
            // 更改后，就统一 this.depAreaOptions[0]，后面数据进行关联变动。
            this.ruleForm.depArea = this.depAreaOptions[0] || this.ruleForm.depArea
          }
          // 检查部门
          this.getDep()
          this.getDepartment()
        }
      })
    },
    changeDepArea() {
      // if (!this.ruleForm.depArea) return
      // 关联：检查部门、科室
      this.getDep()
      this.getDepartment()
    },
    getDepartment() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        DepArea: this.ruleForm.depArea,
        type: '科室'
      }
      getItemList(data).then(res => {
        if (res && res.code === 10000) {
          this.departmentOptions = res.content.list.map(_item => _item.item)
          if (this.initalFlag) {
            // 如果进来尚无更改，默认是this.ruleForm.depArea
            this.ruleForm.department = this.ruleForm.department || this.departmentOptions[0]
          } else {
            // 更改后，就统一 this.depAreaOptions[0]，后面数据进行关联变动。
            this.ruleForm.department = this.departmentOptions[0] || this.ruleForm.department
          }
        }
      })
    },
    getDep() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        DepArea: this.ruleForm.depArea
      }
      getDepList(data).then(res => {
        if (res && res.code === 10000) {
          this.depOptions = res.content.list
          if (this.initalFlag) {
            this.ruleForm.dep = this.ruleForm.dep || this.depOptions[0]
          } else {
            this.ruleForm.dep = this.depOptions[0] || this.ruleForm.dep
          }
          // 检查分组、检查设备、检查机房
          this.getCheckList()
        }
      })
    },
    changeDep() {
      // if (!this.ruleForm.dep) return
      // 关联：检查分组、检查设备、检查机房
      this.getCheckList()
    },
    async getCheckList() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        Dep: this.ruleForm.dep
      }
      await this.getModality(data)
      await this.getCheckGroup(data)
      await this.getCheckRoom(data)
      this.initalFlag = false
    },
    getModality(data) {
      return new Promise((resolve, reject) => {
        getModalityList(data).then(res => {
          if (res && res.code === 10000) {
            this.modalityOptions = res.content.list
            if (this.initalFlag) {
              this.ruleForm.modality = this.ruleForm.modality || this.modalityOptions[0]
            } else {
              this.ruleForm.modality = this.modalityOptions[0] || this.ruleForm.modality
            }
            resolve()
          }
        })
      })
    },
    getCheckGroup(data) {
      return new Promise((resolve, reject) => {
        getCheckGroupList(data).then(res => {
          if (res && res.code === 10000) {
            this.checkGroupOptions = res.content.list
            if (this.initalFlag) {
              this.ruleForm.checkGroup = this.ruleForm.checkGroup || this.checkGroupOptions[0]
            } else {
              this.ruleForm.checkGroup = this.checkGroupOptions[0] || this.ruleForm.checkGroup
            }
            resolve()
          }
        })
      })
    },
    getCheckRoom(data) {
      return new Promise((resolve, reject) => {
        getCheckRoomList(data).then(res => {
          if (res && res.code === 10000) {
            this.checkRoomOptions = res.content.list
            if (this.initalFlag) {
              this.ruleForm.checkRoom = this.ruleForm.checkRoom || this.checkRoomOptions[0]
            } else {
              this.ruleForm.checkRoom = this.checkRoomOptions[0] || this.ruleForm.checkRoom
            }
            resolve()
          }
        })
      })
    },
    handleClose() {
      this.$emit('myClose')
    },
    handleReset() {
      this.ruleForm = {
        checkRoom: '',
        dep: '',
        modality: '',
        checkRoomState: '',
        checkGroup: '',
        depArea: '',
        hospitalCode: '',
        hospitalName: '',
        aeTitle: '',
        department: '',
        modalityAddress: '',
        remark1: ''
      }
    },
    handleConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            // edit
            editCheckroom(this.ruleForm).then(res => {
              if (res && res.code === 10000) {
                this.$emit('myClose', 'refresh')
              }
            })
          } else {
            // add
            let index = this.mx_hospitalList.findIndex(item => item.code === this.ruleForm.hospitalCode)
            this.ruleForm.hospitalName = this.mx_hospitalList[index].name
            addCheckroom(this.ruleForm).then(res => {
              if (res && res.code === 10000) {
                this.$emit('myClose', 'refresh')
              }
            })
          }
        } else {
          setTimeout(() => {
            var isError = document.getElementsByClassName('is-error')
            isError[0].querySelector('input').focus()
          }, 100)
        }
      })
    }

  }
}
</script>
<style lang="sass" scoped>

</style>
